﻿using AbpVnext_Module.DBContext;
using Castle.Components.DictionaryAdapter.Xml;
using IdentityModel.Client;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Security.Principal;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;

namespace AbpVnext_Module.ConnectionStringResolver
{
    /// <summary>
    /// 替换abp默认的数据库连接字符串配置
    /// </summary>
    //[Dependency(ReplaceServices = true)]
    public class ProjectConnectionStringResolver : IConnectionStringResolver //,ITransientDependency
    {
        public AbpDbConnectionOptions AbpDbConnectionOptions { get; set; }

        public IHostEnvironment hostEnvironment { get; set; }

        public ProductDbContext dbContext { get; set; }

        /// <summary>
        /// 配置volo.abp.ecfore 仓储上下文 会出现递归的问题    
        /// </summary>
        //public ProjectDbContext projectContext { get; set; }

        public IConfiguration configuration { get; set; }

        /// <summary>
        /// 获取到请求上下文
        /// </summary>
        public IHttpContextAccessor httpContextAccessor { get; set; }

        public ProjectConnectionStringResolver(IOptionsMonitor<AbpDbConnectionOptions> options)
        {
            AbpDbConnectionOptions = options.CurrentValue;
        }


        public string Resolve(string? connectionStringName = null)
        {
            #region 根据用户id返回连接字符串
            {
                //var x = httpContextAccessor.HttpContext.User.FindUserId();
                //AbpDbConnectionOptions.ConnectionStrings.GetOrDefault("userid");
            }
            #endregion

            #region 根据环境返回请求连接字符串
            {
                //if (hostEnvironment.IsDevelopment())
                //{
                //    return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault(hostEnvironment.EnvironmentName);
                //}
                //else if (hostEnvironment.IsProduction())
                //{
                //    return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault(hostEnvironment.EnvironmentName);
                //}
                //return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault(connectionStringName);
            }
            #endregion

            #region 根据数据库类型切换
            {
                //string config = configuration.GetSection("DatabaseType").Value;
                //if (config.Equals("SqlServer"))
                //{
                //    return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault("Order_Sqlserver");
                //}
                //else
                //{
                //    return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault("Order");
                //}
            }
            #endregion

            #region 默认返回字符串
            {
                return AbpDbConnectionOptions.ConnectionStrings.GetOrDefault(connectionStringName);
            }
            #endregion
        }

        public Task<string> ResolveAsync(string? connectionStringName = null)
        {
            var str = Resolve(connectionStringName);
            //Console.Write(str);
            return Task.FromResult(str);
        }
    }
}
